package edu.ccut.saturn.component.view;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.ccut.saturn.component.ILogger;
import edu.ccut.saturn.component.IView;
import edu.ccut.saturn.component.SaturnLoggerManager;

/**
 * xml类型视图的实现类，保存了用于响应用户操作的xml字符类型的数据。
 * 
 * @author 谷钰
 * 
 * @version 1.0 2008.8.28
 * 
 * @see IView
 * @see HttpServletRequest
 * @see HttpServletResponse
 * 
 */
public class XmlView implements IView {

	private static final String LOGGER_KEY_BUNDLE_RUNTIME = SaturnLoggerManager.LOGGER_KEY_COMPONENT_RUNTIME;

	protected static ILogger logger = SaturnLoggerManager.getInstance()
			.getSaturnLogger(LOGGER_KEY_BUNDLE_RUNTIME);

	private String xmlData = null;

	public XmlView(String xmlData) {
		this.xmlData = xmlData;
	}

	public void dispather(HttpServletRequest request,
			HttpServletResponse response) {
				
		if (this.xmlData != null) {
			try {		
				response.setContentType("text/xml; charset=UTF-8");
				response.getWriter().write(this.xmlData);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {		
				try {
					response.flushBuffer();
				} catch (IOException ie) {
					ie.printStackTrace();
				}
			}
		} else {
			logger.error("Error! try to write [null] to page!");
		}
	}
}
